/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.token.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opgea.token.dao.TokenDAO;
import com.opgea.token.domain.constants.ApplicationConstants;
import com.opgea.token.service.AccessService;
import com.opgea.token.service.ApplicationService;
import com.opgea.token.service.CompanyService;
import com.opgea.token.service.LoginService;
import com.opgea.token.util.FacesUtil;
import com.opgea.token.web.dto.CompanyDTO;
import com.opgea.token.web.dto.LoginDTO;

/**
 * 
 * @author Ramesh
 */

@Component
@Scope(value = "request")
@ManagedBean(name = "loginController")
@RequestScoped
public class LoginController {

    private Logger logger = Logger.getLogger(LoginController.class);

    @Autowired
    private LoginService loginService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private ApplicationService applicationService;

    @Autowired
    private TokenDAO tokenDAO;

    private CompanyDTO companyDTO = new CompanyDTO();
    private LoginDTO loginDTO = new LoginDTO();
    private List<LoginDTO> loginDTOList = new ArrayList<LoginDTO>();

    public CompanyDTO getCompanyDTO() {
	return companyDTO;
    }

    public void setCompanyDTO(CompanyDTO companyDTO) {
	this.companyDTO = companyDTO;
    }

    public LoginDTO getLoginDTO() {
	return loginDTO;
    }

    public void setLoginDTO(LoginDTO loginDTO) {
	this.loginDTO = loginDTO;
    }

    public List<LoginDTO> getLoginDTOList() {
	return loginDTOList;
    }

    public void setLoginDTOList(List<LoginDTO> loginDTOList) {
	this.loginDTOList = loginDTOList;
    }

    public String signUp() {
	companyService.create(companyDTO);
	return "success.xhtml";
    }

    public String save() {
	loginService.create(loginDTO);
	return "success.xhtml";
    }

    public String view() {
	return "view.xhtml";
    }

    public String edit() {
	ExternalContext extContext = FacesContext.getCurrentInstance()
		.getExternalContext();
	Map<String, String> params = extContext.getRequestParameterMap();
	String loginId = (String) params.get("loginId");
	this.loginDTO = loginService.find(new Long(loginId));
	return "create.xhtml";
    }

    public String list() {
	return "list.xhtml";
    }

    public String verify() {
	logger.info("Inside Verify...");

	Map<String, Object> responseMap = loginService.verify(
		loginDTO.getLoginId(), loginDTO.getPassword());
	if (responseMap.get(ApplicationConstants.LOGIN_ID) == Boolean.TRUE
		&& responseMap.get(ApplicationConstants.PASSWORD) == Boolean.TRUE) {
	    LoginDTO dto = (LoginDTO) responseMap
		    .get(ApplicationConstants.OBJECT);
	    FacesUtil.getSessionUserDataObject().setLoginId(dto.getLoginId());
	    FacesUtil.getSessionUserDataObject().setLoginIdPrimaryKey(
		    dto.getId());
	    FacesUtil.getSessionUserDataObject().setUserType(dto.getUserType());
	    FacesUtil.getSessionUserDataObject().setCompanyId(
		    dto.getCompanyId());
	    FacesUtil.getSessionUserDataObject().setCompanyName(dto.getCompanyName());
	    FacesUtil.getSessionUserDataObject().setEmpId(dto.getEmployeeId());
	    FacesUtil.getSessionUserDataObject().setDestinationId(dto.getDestinationId());
	    FacesUtil.getSessionUserDataObject().setEmployeeDestination(dto.getDestinationId());
	    FacesUtil.getSessionUserDataObject().setEmployeeDestinationString(dto.getDestinationName());
	    FacesUtil.getSessionUserDataObject().setPageList(
		    AccessService.getPageList());
	    FacesUtil.getSessionUserDataObject().setActionList(
		    AccessService.getActionList(1, 1));
	    logger.info("View: welcome");
	    return "../welcome/welcome.xhtml";
	}
	if (responseMap.get(ApplicationConstants.LOGIN_ID) == Boolean.FALSE) {
	    FacesUtil.addMessage(ApplicationConstants.MESSAGES,
		    FacesMessage.SEVERITY_ERROR, "Invalid User Id!", null);
	    return "login.xhtml";
	} else {
	    FacesUtil.addMessage(ApplicationConstants.MESSAGES,
		    FacesMessage.SEVERITY_ERROR, "Invalid Password!", null);
	    logger.info("View: login");
	    return "login.xhtml";
	}
    }

    public String logout() {
	FacesUtil.getHttpSession().invalidate();
	return "/login/login.xhtml?faces-redirect=true";
    }
}
